
function DrawBillboardByDegrees(name,lon,lat,mURL,description,pointList,dataid) {
    var positions = Cesium.Cartographic.fromDegrees(lon,lat);
    Cesium.when(new Cesium.sampleTerrain(viewer.terrainProvider, 13, [positions]), function (updatedPositions) {
        var entity = viewer.entities.add({
            name:name,
            position: Cesium.Cartesian3.fromDegrees(lon,lat,updatedPositions[0].height),
            billboard: {
                show: true, // default
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM
            }
        });

        entity.dataid=dataid;
        entity.billboard.image=mURL;
        if(description!="")
            entity.description=description;

        pointList.push(entity);
    });
}

//绘制标签
function DrawBillboard(name,lon,lat,mURL,description) {
    var entity = viewer.entities.add({
        name:name,
        position:Cesium.Cartesian3.fromDegrees(lon,lat),
        billboard: {
            show: true, // default
            disableDepthTestDistance:Number.POSITIVE_INFINITY,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM
        }
    });

    entity.billboard.image=mURL;
    if(description!="")
        entity.description=description;

    return entity;
}

//多边形或体
function DrawPloygon(name,mPoints,materialT,description,dataid) {
    var mhierarchy = new Cesium.PolygonHierarchy();
    $.each(mPoints, function (i, value) {
        mhierarchy.positions.push(new Cesium.Cartesian3.fromDegrees(value.x,value.y,0));
    });

    var entity=entities.add({
        name:name,
        polygon: {
            hierarchy: mhierarchy,
            perPositionHeight:false,
            material: materialT
        }
    });
    if(description!="")
        entity.description=description;

    entity.dataid=dataid;

    return entity;
}


//图层数据控制
function loadJsonData(pname,purl, pstroke, pfill, pstrokeWidth) {
    var JsonDataSource=new Cesium.GeoJsonDataSource(pname);
    JsonDataSource.load(purl, {
        markerSize:30,
        stroke: pstroke,
        fill: pfill,
        strokeWidth: pstrokeWidth,
        markerSymbol: 'i',
        clampToGround:true
    });
    dataSources.add(JsonDataSource);
    return JsonDataSource;
};

function Load3DTiles(tilesetUrl){
    var tileset= scene.primitives.add(new Cesium.Cesium3DTileset({
        url :tilesetUrl,
        cullWithChildrenBounds:false,
        skipLevelOfDetail:true,
        maximumMemoryUsage:1024,
        oadSiblings:true
    }));
    set_3DTileHeight(tileset,0);
    return tileset;
};

function Remove3DTiles(tilesetUrl) {
    var tileset = this.Get3DTilesByUrl(tilesetUrl);
    scene.primitives.removeAndDestroy(tileset);
};
function Get3DTilesByUrl(tilesetUrl) {
    if(!isNaN(tilesetUrl)){//数字
        tilesetUrl=tilesetUrl+"/tileset.json";
    }
    for (var i = 0; i < scene.primitives.length; i++) {
        var tileset = scene.primitives.get(i);
        if (tileset.url != undefined && tileset.url.indexOf(tilesetUrl)>=0) {
            return tileset;
        }
    }
    return null;
};
function set_3DTileHeight(tileset,Height){

    if(tileset.primitive!=undefined){
        heightOffset += Height;
        var boundingSphere = tileset.primitive.boundingSphere;
        var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
        var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
        var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, heightOffset);
        var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
        tileset.primitive.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
    }else
    {
        tileset.readyPromise.then(function(tileset) {
            var boundingSphere = tileset.boundingSphere;
            var cartographic = Cesium.Cartographic.fromCartesian(boundingSphere.center);
            var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
            var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, Height);
            var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
            tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
            heightOffset=Height;
        });
    }

};

function flyIntoObject(object) {
    if (object._tilesetUrl != undefined) {
        this.zoomToTileset(object);
    } else {
        viewer.flyTo(object);
    }
};
